import Vue from 'vue';
import Vuex from 'vuex'
import detail from './detail'
import shopCart from "./shopCart";
import user from "./user";
import trade from "./trade";
import {reqCategory,reqGetBannerList,reqGetFloorList,reqGetSearchInfo} from "../api";
Vue.use(Vuex)
const state={
  categoryList:[],
  bannerList:[],
  floorList:[],
  searchList:{}//是对象还是数组有服务器返回的数据来决定的
};
  const mutations={
    CATEGORYLIST(state,categoryList){
     state.categoryList=categoryList
    },
    GETBANNERLIST(state,bannerList){
      state.bannerList=bannerList
    },
    GETFLOORLIST(state,floorList){
      state.floorList=floorList
    },
    GETSEARCHLIST(state,searchList){
      state.searchList=searchList.data//这里一定要data不然在下面的getters返回值下面加的话会出错(访问不到值)
    }
  };
  const actions={
    async categoryList({commit}){
      let result =await reqCategory();
      if(result.status =="200") {
        commit("CATEGORYLIST", result.data)
      }
    },
    async getBannerList({commit}){
      let result=await reqGetBannerList();
        commit("GETBANNERLIST",result.data)
        console.log(result)
    },
    async getFloorList({commit}){
      let result=await reqGetFloorList();
      commit("GETFLOORLIST",result.data)
    },
    async getSearchList({commit},params={}){//action的第二个参数,让他默认为空对象
      let result=await reqGetSearchInfo(params);
      commit("GETSEARCHLIST",result.data)
    }
  };
const  getters={
    getGoodsList(state){
      return state.searchList.goodsList
    },
    getTrademarkList(state){
      return state.searchList.trademarkList
    },
    getAttrsList(state){
      return state.searchList.attrsList
    }
  }
  const modules={
  detail,
    shopCart,
    user,
    trade
  }
export  default new Vuex.Store({
  state,
  mutations,
  actions,
  getters,
  modules,
})
